package com.das.mechanic_base.widget;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3StringUtils;
import com.umeng.analytics.MobclickAgent;

/**
 * Created by chensi on 2018/4/24.
 */

public class X3NoticeDialog extends Dialog {

    private Point mScreenPoint = new Point();
    private Activity mRootActivity = null;

    private TextView tv_title;
    private Button btn_content;

    private String title;
    private String content;

    /**
     * 一个按钮Dialog
     *
     * @param arg
     * @param title
     * @param content
     */
    public X3NoticeDialog(Activity arg, String title, String content) {
        super(arg, R.style.pointDialog);
        mRootActivity = arg;
        this.title = title;
        this.content = content;
    }

    public void setTitle(String text) {
        title = text;
        if (tv_title != null) {
            tv_title.setText(title);
        }
    }

    public void setDescription(String text) {
        content = text;
        if (!X3StringUtils.isEmpty(content)) {
            btn_content.setText(content);
        }else {
            getView(R.id.v_line).setVisibility(View.GONE);
            btn_content.setVisibility(View.GONE);
        }
    }

    /**
     * 通过控件的Id获取对于的控件，如果没有则加入views
     *
     * @param viewId
     * @return
     */
    public <T extends View> T getView(int viewId) {
        View view = null;
        view = findViewById(viewId);
        return (T) view;
    }

    @SuppressWarnings("deprecation")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setBackgroundDrawable(new BitmapDrawable());
        this.setContentView(R.layout.x3_notice_dialog);
        setCancelable(isTouchCancel);
        setCanceledOnTouchOutside(isTouchCancel);
        initDialogWindow();
        initView();
    }

    private boolean isTouchCancel;

    public void setTouchCancel(boolean isCancel) {
        this.isTouchCancel = isCancel;
    }

    public Button getBtn_content () {
        return btn_content;
    }

    void initView() {
        tv_title = findViewById(R.id.tv_title);
        btn_content = findViewById(R.id.btn_content);
        setTitle(title);
        setDescription(content);
        btn_content.setOnClickListener(view -> dismiss());
    }

    @SuppressWarnings("deprecation")
    private void initDialogWindow() {
        Window dialogWindow = getWindow();
        dialogWindow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);

        WindowManager m = mRootActivity.getWindowManager();
        Display d = m.getDefaultDisplay(); // 获取屏幕宽、高用

        try {
            d.getSize(mScreenPoint);
        } catch (NoSuchMethodError ignore) {
            mScreenPoint.x = d.getWidth();
            mScreenPoint.y = d.getHeight();
        }

        WindowManager.LayoutParams p = dialogWindow.getAttributes(); // 获取对话框当前的参数值
        p.width = (int) (mScreenPoint.x * 0.80);    // 宽度设置为屏幕的0.9
        dialogWindow.setAttributes(p);
    }
    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("验证码发送成功弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("验证码发送成功弹窗");
    }
}


